package com.gitee.hljdrl.wave;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.ColorInt;

import com.gitee.hljdrl.wave.basis.WaveEvent;

import java.util.ArrayList;
import java.util.List;

public class WaveDrawEvent implements WaveEvent {

    private Paint mPaint;
    // 透明度比例，按照扩散圆范围，递减
    private float mAlphaRate;
    private boolean isStartAnim = false;
    // 圆心
    private float mCenterX;
    private float mCenterY;
    private int mCenterColor;
    // 新增圆的节点，到达此点增加一个圆
    private int mSpace;
    // 圆扩散宽度
    private int mWidth;
    // 圆是否填充满
    protected boolean isFillStyle;
    protected boolean isProceed = false;
    private int mWaveMax;
    private int mWaveMultiple;
    private int mWaveLineStrokeWidth;
    private final List<Float> mCount = new ArrayList<>();
    private final List<Integer> mAlphas = new ArrayList<>();

    @Override
    public void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveView);
        mCenterColor = typedArray.getColor(R.styleable.WaveView_wave_color, Color.BLUE);
        mSpace = typedArray.getDimensionPixelSize(R.styleable.WaveView_wave_space, 100);
        isFillStyle = typedArray.getBoolean(R.styleable.WaveView_wave_fill_style, true);
        isProceed = typedArray.getBoolean(R.styleable.WaveView_wave_proceed, false);
        mWidth = typedArray.getDimensionPixelSize(R.styleable.WaveView_wave_width, 400);
        mWaveMax = typedArray.getInteger(R.styleable.WaveView_wave_max_circle, 8);
        int multiple = typedArray.getInteger(R.styleable.WaveView_wave_multiple, 1);
        mWaveLineStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.WaveView_wave_line_stroke_width, 3);
        typedArray.recycle();
        //------------------------------------------------------------------------------------
        final int dp_px = dp2px(context, 1);
        //TODO 不同分辨率，绘制光圈速度会有差距.
        mWaveMultiple = multiple+dp_px;
        //------------------------------------------------------------------------------------
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        if (isFillStyle) {
            mPaint.setStyle(Paint.Style.FILL);
        } else {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(mWaveLineStrokeWidth);
        }
        mAlphaRate = 255f / mWidth; //注意这里 如果为int类型就会为0,除数中f一定要加,默认int ;
        mAlphas.add(255);
        mCount.add(0f);
        //
    }

    protected int dp2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    @Override
    public void startAnimation() {
        if (!isStartAnim) {
            isStartAnim = true;
        }
    }

    @Override
    public void stopAnimation() {
        if (isStartAnim) {
            isStartAnim = false;
            reset();
        }
    }


    protected void reset() {
        if (isProceed) {
            return;
        }
        try {
            mAlphas.clear();
            mCount.clear();
            //
            mAlphas.add(255);
            mCount.add(0f);
        } catch (Exception ex) {
            //TODO
        }
    }

    @Override
    public boolean isRunning() {
        return isStartAnim;
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCenterX = w / 2;
        mCenterY = h / 2;
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (!isStartAnim) {
            return;
        }
        try {
            mPaint.setColor(mCenterColor);
            for (int i = 0; i < mCount.size(); i++) { //遍历圆数目
                Integer cuAlpha = mAlphas.get(i);
                mPaint.setAlpha(cuAlpha);
                Float aFloat = mCount.get(i);
                canvas.drawCircle(mCenterX, mCenterY, aFloat, mPaint); //画圆
                if (aFloat < mWidth) {  //扩散直径和透明度
                    mAlphas.set(i, (int) (255 - mAlphaRate * aFloat));
                    //TODO 倍数
                    mCount.set(i, aFloat + mWaveMultiple);
                }
                //Log.d("WaveWiew", "onDraw: "+cuAlpha+" "+aFloat);
            }
            // 始终保持仅有五个圆扩散
            if (mCount.size() >= mWaveMax) {
                mAlphas.remove(0);
                mCount.remove(0);
            }
            // 如果圆到达此点，增加一个圆
            if (mCount.get(mCount.size() - 1) >= mSpace) {
                mAlphas.add(255);
                mCount.add(0f);
            }
        } catch (Exception ex) {
            //TODO
        }
    }

    @Override
    public void setWaveColor(@ColorInt int color) {
        mCenterColor = color;
    }

}
